﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFMINERP.PrintShopBill.Iview;
using WCFMINERP.InnerFactoryContracts.DataContracts;
using WCFMINERP.PrintShopBill.Presenter;
using WCFMINERP.COM;

namespace WCFMINERP.PrintShopBill.Forms
{
    public partial class FrmMain : Form,IMainView
    {
        private MainPresenter _presenter = null;
        public FrmMain()
        {
            InitializeComponent();
            _presenter = new MainPresenter(this);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string strProdCode =txtOrderInfoBarcode.Text.Trim();
            if (strProdCode.Trim() == "")
            {
                return;
            }
            if (strProdCode.Length != 10)
            {
                strProdCode = "2" + string.Format("{0:d9}", Convert.ToInt32(strProdCode));
            }
            ReciveBarcode(strProdCode);
        }
        private void ReciveBarcode(string barcode)
        {
            try
            {
                string errorMsg = "";
                int ret = _presenter.ReceiveBarcode(barcode, out errorMsg);
                if (ret != 0)
                {
                    MessageBox.Show(errorMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    txtOrderInfoBarcode.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 绑定待打印账单的产品列表
        /// </summary>
        /// <param name="orderInfo"></param>
        public void LinkOrderInfo(List<OrderInfo_Info> orderInfoList)
        {
            gcOrderInfo.DataSource = orderInfoList;
            gcOrderInfo.RefreshDataSource();
        }

        private void btnPrintShopBill_Click(object sender, EventArgs e)
        {
            try
            {
                List<OrderInfo_Info> orderList = gcOrderInfo.DataSource as List<OrderInfo_Info>;
                if (orderList!=null||orderList.Count > 0)
                {
                    _presenter.PrintShopBill(InnerFactoryParameter.TechId, InnerFactoryParameter.UserInfo.UserId, orderList, true, 0);
                }
                else
                {
                    MessageBox.Show("无账单可以打印！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch( Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }

        private void btnReprint_Click(object sender, EventArgs e)
        {

            try
            {
                _presenter.RePrint();

            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void FrmMain_KeyPress(object sender, KeyPressEventArgs e)
        {

                if (e.KeyChar == (char)13)//用户输入回车键
                {
                    try
                    {
                        string strBarCode = txtOrderInfoBarcode.Text.Trim();

                        if (strBarCode.Length != 10)
                        {
                            strBarCode = "2" + string.Format("{0:d9}", Convert.ToInt32(strBarCode));
                        }
                        this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
                        string errorMsg = "";
                        int ret = _presenter.ReceiveBarcode(strBarCode, out errorMsg);
                        if (ret != 0)
                        {
                            MessageBox.Show(errorMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            txtOrderInfoBarcode.Text = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
           
           
            

        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if( DialogResult.OK !=MessageBox.Show("确定要退出系统吗？","询问",MessageBoxButtons.OKCancel,MessageBoxIcon.Question))
            {
                e.Cancel = true;
            }
        }
    }
}
